package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 粤海农垦知青博客
 * @author kd-15
 */
public class YhnkzqBlogAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://blog.yhnkzq.com/u/.*/archives/[0-9]*/[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		Integer deliverNum = null;
		String updateUrl = url;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("td#content9").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("td.content9").text();
		}

		date = DateUtils.matchDate(doc.select("span.textbox-label").text()) ;
		if(date==null){
			date = DateUtils.matchDate(doc.select("html body table.content2 tbody tr td div#content table tbody tr td table tbody tr td table.seyle4 tbody tr td div").text()) ;
		}

		content = HtmlCleaner.getContentHtml(url,  doc.select("span.oblog_text"));
		author = doc.select("td.blog_table_t1").text();
		clickNum =NumberUtils.matchNumber(doc.select("a:containsOwn(阅读全文").text());
		commnetNum =NumberUtils.matchNumber(doc.select("a:containsOwn(回复").text());
		deliverNum = NumberUtils.matchNumber(doc.select("a:containsOwn(引用通告").text());

		NewsMeta news = new NewsMeta();
		news.setUrl(url);
		news.setType(7);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(commnetNum);
		news.setClickNum(clickNum);
		news.setAuthor(author);
		//news.setDeliverNum(deliverNum);
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setUpdateUrl(updateUrl);
		return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	

	
}
